﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Linq;
using System.Linq;
using System.Net.Http;

namespace eventful.ViewModels
{
	public class MainViewModel : INotifyPropertyChanged
	{
		public MainViewModel()
		{
			this.Items = new ObservableCollection<ItemViewModel>();
		}

		/// <summary>
		/// A collection for ItemViewModel objects.
		/// </summary>
		public ObservableCollection<ItemViewModel> Items { get; private set; }
		public ObservableCollection<ItemViewModel> Venues { get; private set; }
		public ObservableCollection<ItemViewModel> Query { get; private set; }

		private string _sampleProperty = "";
		/// <summary>
		/// Sample ViewModel property; this property is used in the view to display its value using a Binding
		/// </summary>
		/// <returns></returns>
		public string ContextTitle
		{
			get
			{
				return _sampleProperty;
			}
			set
			{
				if( value != _sampleProperty )
				{
					_sampleProperty = value;
					NotifyPropertyChanged( "ContextTitle" );
				}
			}
		}

		private bool _network = true;
		/// <summary>
		/// Sample ViewModel property; this property is used in the view to display its value using a Binding
		/// </summary>
		/// <returns></returns>
		public bool Network
		{
			get
			{
				return _network;
			}
			set
			{
				if( value != _network )
				{
					_network = value;
					NotifyPropertyChanged( "Network" );
				}
			}
		}

		public bool IsDataLoaded
		{
			get;
			private set;
		}

		/// <summary>
		/// Creates and adds a few ItemViewModel objects into the Items collection.
		/// </summary>
		public async void LoadData()
		{
			var isConnected = Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable;
			if( isConnected )
			{
				var client = new HttpClient();

				try
				{
					var response = await client.GetStringAsync( App.BLOB_CONTAINER + "popular" );
					var events = XDocument.Parse( response );
					Items = new ObservableCollection<ItemViewModel>(
						from venue in events.Root.Element( "events" ).Elements( "event" )
						select new ItemViewModel
						{
							LineOne = venue.Element( "title" ).Value,
							LineTwo = venue.Element( "venue_name" ).Value,
							LineThree = venue.Element( "image" ).Element( "medium" ).Element( "url" ).Value,
							ExternalUrl = venue.Element( "url" ).Value,
							ID = venue.Attribute( "id" ).Value
						} );
					NotifyPropertyChanged( "Items" );
				}
				catch( HttpRequestException e )
				{
					Network = false;
				}

				try
				{
					var response = await client.GetStringAsync( App.BLOB_CONTAINER + "topvenues" );
					var xmlVenues = XDocument.Parse( response );
					Venues = new ObservableCollection<ItemViewModel>(
						from venue in xmlVenues.Root.Element( "venues" ).Elements( "venue" )
						select new ItemViewModel
						{
							LineOne = venue.Element( "name" ).Value,
							LineTwo = venue.Element( "description" ).Value,
							LineThree = venue.Element( "image" ).Element( "medium" ) == null ? null : venue.Element( "image" ).Element( "medium" ).Element( "url" ).Value,
							ExternalUrl = venue.Element( "url" ).Value,
							ID = venue.Attribute( "id" ).Value
						} );
					NotifyPropertyChanged( "Venues" );
					IsDataLoaded = true;
				}
				catch( HttpRequestException e )
				{
					Network = false;
				}
			}
		}

		public async void LoadQuery( string url )
		{
			var isConnected = Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable;

			if( isConnected )
			{
				if( Query != null )
					Query.Clear();
				var client = new HttpClient();
				try
				{
					var response = await client.GetStringAsync( url );
					var xmlVenues = XDocument.Parse( response );
					Query = new ObservableCollection<ItemViewModel>(
						from item in xmlVenues.Root.Element( "events" ).Elements( "event" )
						where item.Element( "image" ).Element( "medium" ) != null
						select new ItemViewModel
						{
							LineOne = item.Element( "title" ).Value,
							LineTwo = item.Element( "venue_name" ).Value,
							LineThree = item.Element( "image" ).Element( "medium" ).Element( "url" ).Value,
							ExternalUrl = item.Element( "url" ).Value,
							ID = item.Attribute( "id" ).Value
						} );

					NotifyPropertyChanged( "Query" );
				}
				catch( HttpRequestException e )
				{
					Network = false;
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
		private void NotifyPropertyChanged( string propertyName )
		{
			var handler = PropertyChanged;
			if( null != handler )
			{
				handler( this, new PropertyChangedEventArgs( propertyName ) );
			}
		}
	}
}